import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_shop_list(self):

        #点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()

        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)

        self.browser.find_element(By.XPATH,'//*[@id="keyword"]').send_keys('文具')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()

        #接收列表
        tr_list =self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        #接收条款
        tr_count =len(tr_list)
        if tr_count == 0:
            print('没有数据')
        is_not_bug = True
        for i in tr_list:
            shop_name =i.find_element(By.XPATH,'./td[2]/div').text
            if '文具' not in shop_name:
                is_not_bug = False
        if  is_not_bug:
            print('商品列表查询没有问题')
        else:
            print('商品列表有问题')
        sleep(2)
    def test_login(self):
         self.browser.get('http://likestop/admin/account/login.html')
         sleep(2)

        #输入用户名
         self.browser.find_element(By.NAME,'account').send_keys('admin')

        #输入密码
         self.browser.find_element(By.NAME,'password').send_keys('admin')

        #输入验证码
         self.browser.find_element(By.NAME,'code').send_keys('11111')

        #点击登录
         self.browser.find_element(By.ID,'login').click()
         sleep(3)


    def test_shop_add(self):
         #点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()

        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        #定位发布商品按钮
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        
        now = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        #定位商品发布子框架   
        shop_add_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe)
        
        #定位商品名称
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品'+now)

        #点击第一个大类的下拉框
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()

        #点击下拉框的下拉数据的第一个数据
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()

        #点击第一个大类的下拉框
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()

        #点击下拉框的下拉数据的第一个数据
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()

        #点击第一个大类的下拉框
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()

        #点击下拉框的下拉数据的第一个数据
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()

        #点位商品主图
        #定位商品主图按钮
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)

        #进入上传商品子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        self.test_upload_img('C:\\Users\\86133\\Documents\\网页设计\\软件测试\\软件测试.png')

        #商品轮播图
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)

        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)
        #进入图片上传子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('C:\\Users\\86133\\Documents\\网页设计\\软件测试\\软件测试.png')
        
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)
        #点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)

        #定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        sleep(3)
        
        # 点击保存按钮，因为保存是在商品子框架，所以需要回到商品子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        self.browser.switch_to.default_content()

    def test_upload_img(self,file_path):
        #图片上传
        self.browser.find_element(By.XPATH,'/html/body/div[2]/div[1]/input').send_keys(file_path)
        #send_keys(file_path)
        sleep(5)
        
        #选中第一张图片
        self.browser.find_element(By.XPATH,'//*[@id="lists"]/li[1]/div/img').click()
        sleep(1)
        #点击使用图片按钮
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(2)

       
    def test_shop_sj(self):
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位子框架
        iframel = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframel)

        #点击仓库中的商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)

        #获取商品列表
        tr_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        
        if len(tr_list) > 0:
            #上架列表第一个商品
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(1)
            #点击确定
            self.browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/a[1]').click()
            sleep(2)
            self.browser.switch_to.default_content()
    def test_logout(self):
        
        self.browser.switch_to.default_content()
        dt = self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(5)
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        
    def test_shop_edit(self):
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位子框架
        iframel = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframel)

        #点击仓库中的商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        now = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[1]').click()
        sleep(2)
        
        edit_iframel = self.browser.find_element(By.XPATH,'/html/body/div/div/iframe')
        self.browser.switch_to.frame(edit_iframel)
        #先清除在输入
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
        
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('第一商品'+now)
        
        sleep(5)
        
         #点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
         #定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').clear()
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').clear()
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').clear()
         
         
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('5')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('5')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('5')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('10')
        sleep(3)
        
        
        
        
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(edit_iframel)
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        




if __name__ == '__main__':
    LikeShopTest = LikeShopTest()
    LikeShopTest.test_login()
    #LikeShopTest.test_shop_list()
    LikeShopTest.test_shop_add()
    LikeShopTest.test_shop_sj()
    LikeShopTest.test_shop_edit()
    LikeShopTest.test_logout()
